﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using PeppeDotNet.SP2010.Html5Fields.Controls;
using System.Globalization;
using PeppeDotNet.SP2010.Html5Fields.Number;

namespace PeppeDotNet.SP2010.Html5Fields
{
    public class BaseHtml5InputFieldControl : BaseFieldControl
    {
        #region Fields
        private Html5InputControl control; 
        #endregion

        #region Properties
        public Html5InputType InputType { get; private set; } 
        #endregion

        #region Constructor
        public BaseHtml5InputFieldControl(Html5InputType type)
        {
            InputType = type;
        } 
        #endregion

        protected override void CreateChildControls()
        {
            if (ControlMode == SPControlMode.Display) return;

            base.CreateChildControls();

            switch (InputType)
            {
                case Html5InputType.Text:
                    control = new Html5TextInputControl();
                    FormatLongInput(control);
                    break;
                case Html5InputType.Mail:
                    control = new Html5MailInputControl();
                    FormatLongInput(control);
                    break;
                case Html5InputType.Number:
                    control = new Html5NumberInputControl()
                    {
                        Max = ((Html5NumberInputField)Field).MaximumValue == double.MaxValue ? default(double?) : ((Html5NumberInputField)Field).MaximumValue,
                        Min = ((Html5NumberInputField)Field).MinimumValue == double.MinValue ? default(double?) : ((Html5NumberInputField)Field).MinimumValue,
                        Step = (((Html5NumberInputField)Field).Step != 0) ? ((Html5NumberInputField)Field).Step : 1
                    };
                    FormatShortInput(control);
                    break;
                default:
                    control = new Html5TextInputControl();
                    FormatLongInput(control);
                    break;
            }

            control.ID = String.Format("{0}Control", InputType);            
            
            Controls.Add(control);
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return control.Value;
            }
            set
            {
                EnsureChildControls();
                control.Value = String.Format(CultureInfo.InvariantCulture, "{0}", value);
            }
        }

        private void FormatLongInput(Html5InputControl control)
        {
            control.MaxLength = 255;
            control.Attributes.Add("class", "ms-long ms-spellcheck-true");
        }

        private void FormatShortInput(Html5InputControl control)
        {
            control.Size = 11;
            control.Attributes.Add("class", "ms-input");
        }
    }
}
